package com.nolon.flange.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.nolon.flange.R
import com.nolon.flange.adapter.RawAdapter
import com.nolon.flange.adapter.SolutionAdapter
import com.nolon.flange.databinding.FragmentFlangeCalcBinding
import com.nolon.flange.viewmodel.FlangeViewModel
import com.nolon.flange.viewmodel.SolutionViewModel
import com.google.android.material.bottomsheet.BottomSheetBehavior

class FlangeCalcFragment(private val parent: AppCompatActivity) : Fragment() {
    private lateinit var _binding: FragmentFlangeCalcBinding
    private val bd get() = _binding

    private val rawAdapter: RawAdapter by lazy { RawAdapter(parent) }
    private val solutionAdapter by lazy { SolutionAdapter(parent) }
    private val sheetBehavior by lazy { BottomSheetBehavior.from(bd.cardBottom) }

    // get ViewModel from parent
    private val flangeViewModel by lazy { ViewModelProvider(parent).get(FlangeViewModel::class.java) }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentFlangeCalcBinding.inflate(inflater, container, false)
        return bd.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView()

        initData()

    }

    private fun initData() {
        val solutionViewModel = SolutionViewModel()

        bd.solution = solutionViewModel

        with(flangeViewModel) {

            this.solutionViewModel = solutionViewModel

            isOrderNotEmpty.observe(viewLifecycleOwner) { isOrderNotEmpty ->
                bd.root.visibility = if (isOrderNotEmpty == true) View.VISIBLE else View.INVISIBLE
            }

            groupedParts.observe(viewLifecycleOwner) {
                solutionAdapter.groupedParts = it
            }

            solutions.observe(viewLifecycleOwner) {
                solutionAdapter.solutions = it
                if (it.isNotEmpty()) {
                    solutionViewModel.resultVisible.set(View.VISIBLE)
                }
//                updateProject(solutions = it)
            }

            project.observe(viewLifecycleOwner) {
                populateResult(it)
            }

            rawInfoList.observe(viewLifecycleOwner) {
                rawAdapter.rawInfoList = it
            }
        }
    }


    private fun initView() {
        bd.recyclerRawSpec.apply {
            adapter = rawAdapter
            layoutManager =
                LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        }

        bd.recyclerSolution.apply {
            adapter = solutionAdapter
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        }

        bd.btnCalculate.setOnClickListener { calculate(it) }

        bd.cardTitle.setOnClickListener {
            if (sheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED) {
                sheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
                bd.imgUpDown.setImageResource(R.drawable.ic_baseline_keyboard_arrow_up_36)
            }
            if (sheetBehavior.state == BottomSheetBehavior.STATE_COLLAPSED) {
                sheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
                bd.imgUpDown.setImageResource(R.drawable.ic_baseline_keyboard_arrow_down_36)
            }
        }

        sheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {
                when (newState) {
                    BottomSheetBehavior.STATE_HIDDEN -> Unit
                    BottomSheetBehavior.STATE_COLLAPSED -> {
                        bd.imgUpDown.setImageResource(R.drawable.ic_baseline_keyboard_arrow_up_36)
                    }
                    BottomSheetBehavior.STATE_EXPANDED -> {
                        bd.imgUpDown.setImageResource(R.drawable.ic_baseline_keyboard_arrow_down_36)
                    }
                    BottomSheetBehavior.STATE_DRAGGING -> Unit
                    BottomSheetBehavior.STATE_HALF_EXPANDED -> Unit
                    BottomSheetBehavior.STATE_SETTLING -> Unit
                }
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit
        })
    }

    private fun calculate(v: View) {
        sheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
        flangeViewModel.calculate(v, rawAdapter.getRawInfo())
    }

    companion object {
        @JvmStatic
        fun newInstance(parent: AppCompatActivity): FlangeCalcFragment {
            return FlangeCalcFragment(parent)
        }
    }
}